#include "cli.h"

int readCLI(int argc, char *argv[], char *strFileTarget, char *strFileSrc, int lenFileNames){
    int i;
    int state = 0;
    int len;
    extern int DEBUG;

    for(i = 0; i < argc; i++){
        //check for "-h"
        if(strcmp(argv[i], "-h") == 0){
            printf("\nSYNTAX: sort -i FILENAME -o FILENAME -v -h");
            printf("\n\t-i FILENAME\t- input file to be sorted");
            printf("\n\t-o FILENAME\t- the output file (comma seperated)");
            printf("\n\t-v\t\t- verbose mode");
            printf("\n\t-h\t\t- help page (the one you're looking at right now)");
            printf("\n");
            //abort app after printing help screen
            return(0);
        }
        //check for "-v"
        if(strcmp(argv[i], "-v") == 0){
            extern int DEBUG;
            state++;
            DEBUG++;
        }
        //check for "-i"
        if(strcmp(argv[i], "-i") == 0){
            len = strlen(argv[i+1]);
            if(len > lenFileNames){printf("\ninvalid length for input filename"); return(0);}
            //printf("\nfile: %s", argv[i+1]);
            strcpy(strFileSrc, argv[i+1]);
            //terminate string
            *(strFileSrc+len) = '\0';
            state++;
        }
        //check for "-o"
        if(strcmp(argv[i], "-o") == 0){
            len = strlen(argv[i+1]);
            if(len > lenFileNames){printf("\ninvalid length for output filename"); return(0);}
            //printf("\nout: %s", argv[i+1]);
            strcpy(strFileTarget, argv[i+1]);
            //terminate string
            *(strFileTarget+len) = '\0';
            state++;
        }
    }

    if(DEBUG){
        for(i = 0; i < lenFileNames; i++){
            printf("\n%2d\t\t%X -> %c\t\t\t%X -> %c", i, strFileSrc[i], (char) strFileSrc[i],strFileTarget[i], (char) strFileTarget[i]);
        }
        getchar();
    }

    return(state);
}
